<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var treeData = [
  {
    "id":1,
    "parent_id":0,
    "name":"四川省",
    "children":[
      {
        "id":5,
        "parent_id":1,
        "name":"成都市",
        "children":[
          {
            "id":6,
            "parent_id":5,
            "name":"锦江区",
            "children":[
              {
                "id":7,
                "parent_id":6,
                "name":"九眼桥"
              },
              {
                "id":8,
                "parent_id":6,
                "name":"兰桂坊"
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "id":2,
    "parent_id":0,
    "name":"广东省",
    "children":[
      {
        "id":9,
        "parent_id":2,
        "name":"东莞市",
        "children":[
          {
            "id":10,
            "parent_id":9,
            "name":"长安镇"
          }
        ]
      }
    ]
  },
  {
    "id":3,
    "parent_id":0,
    "name":"江西省",
    "children":[
      {
        "id":11,
        "parent_id":3,
        "name":"南昌市"
      }
    ]
  }
]	;
		var originchildren;
			function loopTreeData(data){
				data.forEach(function(el){
					if(el.parent_id == 0){
						 originchildren = el.children;
						var originchildrenCopy = JSON.parse(JSON.stringify(el.children));
						originchildren.forEach(function(item){
							if(item.children && item.children.length > 0){
								delete item.children;
							}
						});
						if(originchildrenCopy && originchildrenCopy.length > 0){
							loopTreeData(originchildrenCopy)
						}
					}else{
						if(el.children && el.children.length > 0){
							var originchildren2 = el.children;
							var originchildrenCopy2 = JSON.parse(JSON.stringify(el.children));
							originchildrenCopy2.forEach(function(item){
								if(item.children && item.children.length > 0){
									delete item.children;
									originchildren.push(item);
								}
							});
							loopTreeData(originchildren2);
						}
					}
				})
				
				return data;
			}
			
			console.log(loopTreeData(treeData));
		</script>
	</body>
</html>
